<%@ page language="java" contentType="text/html" pageEncoding="utf-8"%>
<%@include file="/WEB-INF/pages/common/common.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <%@include file="/WEB-INF/pages/common/head.jsp"%>
    <title><spring:message code="title.part"/></title>
    <script type="text/javascript">
        var datagrid;
        var message =$.messager;
        var operateDialog;
        $(function() {
            datagrid=$('#datagrid').datagrid({
                url : '${path}/basedata/part/ajaxList.do',
                onLoadSuccess:function(){
                    datagrid.datagrid('clearSelections');
                },
                queryParams: imonJYRH.serializeObject($("#tableForm *"))
            });
        });
        function stateFormatter(value,row,index){
        <c:forEach var="dict" items="${dictBaseDataStatusList}">
            if(row['state']=='${dict.code}'){
                if(row['state']=='9'){
                    return '<span style="color:red">${dict.name}</span>'
                }else{
                    return '${dict.name}';
                }
            }
        </c:forEach>
        }
        function partTypeFormatter(value,row,index){
            <c:forEach var="dict" items="${dictPartTypeList}">
                if(row['partType']=='${dict.code}'){
                    return '${dict.name}';
                }
            </c:forEach>
        }
        function productTypeFormatter(value,row,index){
            <c:forEach var="dict" items="${dictProductTypeList}">
                if(row['productType']=='${dict.code}'){
                    return '${dict.name}';
                }
            </c:forEach>
        }
        //子页面返回方法
        function returnResult(){
            operateDialog.dialog('close');
            datagrid.datagrid('unselectAll');
            datagrid.datagrid('reload');
        }
        //判断选择单据是否是一条
        function isChooseOne(rows){
            var flag = false;
            if(rows.length >1 ) {
                message.show({
                    msg : "<spring:message code='msg.choose' arguments='"+rows.length+"'/>",
                    title : '<spring:message code='msg.tip'/>'
                });
                flag = false;
            } else if (rows.length == 0) {
                message.show({
                    msg : "<spring:message code='msg.please.choose'/>",
                    title : '<spring:message code='msg.tip'/>'});
                flag = false;
            }else{
                flag = true;
            }
            return flag;
        }
        function getSelectedIds(state){
            var rows = datagrid.datagrid("getSelections");
            if (rows.length > 0) {
                var ids = [];
                for ( var i = 0; i < rows.length; i++) {
                    if(rows[i]['state']!=state){
                        if(state==1){
                            message.alert('<spring:message code='msg.tip'/>','<spring:message code='msg.delete.condition.stop'/>');
                            return null;
                        }else if(state==9){
                            message.alert('<spring:message code='msg.tip'/>','<spring:message code='msg.delete.condition.start'/>');
                            return null;
                        }
                    }
                    ids.push(rows[i].id);
                }
                return ids;
            }else{
                $.messager.alert('<spring:message code='msg.error'/>','<spring:message code='msg.delete'/>','error');
                return null;
            }
        }
        function add_() {
            var href = '${path}/basedata/part/addUI.do';
            operateDialog = $("#operateDialog").dialog({
                title : '<spring:message code='msg.create'/>',
                width : 450,
                height : 450
            });
            $("#operateIFrame")[0].src=href;
            operateDialog.dialog('open');
        }
        function modify_(){
            var rows = datagrid.datagrid("getSelections");
            if(!isChooseOne(rows)){return;}
            var href = '${path}/basedata/part/modifyUI.do?id='+rows[0]['id'];
            operateDialog = $("#operateDialog").dialog({
                title : '<spring:message code='msg.modify'/>',
                width : 450,
                height : 450
            });
            $("#operateIFrame")[0].src=href;
            operateDialog.dialog('open');
        }
        function stop_(){
            var ids = getSelectedIds(1);
            if(ids==null){
                return;
            }
            $.messager.confirm(
                    '<spring:message code='msg'/>',
                    '<spring:message code='msg.confirm.stop'/>',
                    function(r){
                        if (r){
                            imonJYRH.showZone();
                            $.ajax({
                                url : imonJYRH.url("${path}/basedata/part/ajaxStop.do"),
                                cache : false,
                                data :{'ids':ids.join(',')},
                                success : function(reMsg) {
                                    if(reMsg.success){
                                        search_();
                                        message.show({
                                            msg : reMsg.msg,
                                            title : '<spring:message code='msg.tip'/>',
                                            showType:'slide'
                                        });
                                    }else{
                                        message.alert('<spring:message code='msg.tip'/>',reMsg.msg);
                                    }
                                    imonJYRH.hideZone();
                                }
                            });
                        }
                    });
        }
        function start_(){
            var ids = getSelectedIds(9);
            if(ids==null){
                return;
            }
            $.messager.confirm(
                    '<spring:message code='msg'/>',
                    '<spring:message code='msg.confirm.start'/>',
                    function(r){
                        if (r){
                            imonJYRH.showZone();
                            $.ajax({
                                url : imonJYRH.url("${path}/basedata/part/ajaxStart.do"),
                                cache : false,
                                data :{'ids':ids.join(',')},
                                success : function(reMsg) {
                                    if(reMsg.success){
                                        search_();
                                        message.show({
                                            msg : reMsg.msg,
                                            title : '<spring:message code='msg.tip'/>',
                                            showType:'slide'
                                        });
                                    }else{
                                        message.alert('<spring:message code='msg.tip'/>',reMsg.msg);
                                    }
                                    imonJYRH.hideZone();
                                }
                            });
                        }
                    });
        }
        function search_() {
            datagrid.datagrid('unselectAll');
            datagrid.datagrid('load', imonJYRH.serializeObject($("#tableForm *")));
        }
        function reset_(){
            $("#tableForm *").each(function(){
                $(":input:not(:checkbox,:radio)",this).val("");
            });
        }
        /**
         *零件导入
         */
         function import_(){
             var href = '${path}/excel/partExcel/partImportUI.do';
             operateDialog = $("#operateDialog").dialog({
                 title : '<spring:message code='msg.import'/>',
                 width : 800,
                 height : 520
             });
             $("#operateIFrame")[0].src=href;
             operateDialog.dialog('open');
             imonJYRH.showZone();
         }
         //刷新当前页面，目的解决重新导入时不能选择文件
         function reloadFrame(){
       	  window.location = window.location.href;
         }
    </script>
</head>
<body class="easyui-layout">
<div data-options="region:'north'" style="overflow: hidden;height: 90px;">
    <div class="datagrid-toolbar" style="height: auto">
        <br/>
        <fieldset>
            <legend><spring:message code="label.condition"/></legend>
            <table id="tableForm" width="100%">
                <tr>
                    <td align="right"><spring:message code='label.part.code'/>：</td>
                    <td>
                        <input type="text" name="code"/>
                    </td>
                    <td align="right"><spring:message code='label.part.name'/>：</td>
                    <td>
                        <input type="text" name="name"/>
                    </td>
                    <td align="right"><spring:message code="label.part.state"/>：</td>
                    <td>
                        <select name="state">
                            <option value=""><spring:message code='label.all'/></option>
                            <c:forEach var="dict" items="${dictBaseDataStatusList}">
                                <c:choose>
                                    <c:when test='${dict.code==1}'>
                                        <option value="${dict.code}" selected="true">${dict.name}</option>
                                    </c:when>
                                    <c:otherwise>
                                        <option value="${dict.code}">${dict.name}</option>
                                    </c:otherwise>
                                </c:choose>
                            </c:forEach>
                        </select>
                    </td>
                </tr>
            </table>
        </fieldset>
        <br/>
    </div>
</div>
<div data-options="region:'center'">
    <div id="tb">
        <a class="easyui-linkbutton" href="javascript:void(0);" plain="true" iconCls="icon-search" onclick="search_();"><spring:message code="bar.command.search"/></a>
        <a class="easyui-linkbutton" href="javascript:void(0);" plain="true" iconCls="icon-undo" onclick="reset_();"><spring:message code="bar.command.reset"/></a>
        <a class="easyui-linkbutton" href="javascript:void(0);" plain="true" iconCls="icon-add" onclick="add_();"><spring:message code="bar.command.add"/></a>
        <a class="easyui-linkbutton" href="javascript:void(0);" plain="true" iconCls="icon-edit" onclick="modify_();"><spring:message code="bar.command.modify"/></a>
        <a class="easyui-linkbutton" href="javascript:void(0);" plain="true" iconCls="icons-start" onclick="start_();"><spring:message code="bar.command.start"/></a>
        <a class="easyui-linkbutton" href="javascript:void(0);" plain="true" iconCls="icons-stop" onclick="stop_();"><spring:message code="bar.command.stop"/></a>
        <a class="easyui-linkbutton" href="javascript:void(0);" plain="true" iconCls="icon-import" onclick="import_();"><spring:message code="bar.command.import"/></a>
    </div>
    <table id="datagrid" width="100%"
           data-options="toolbar:'#tb',striped:true,fit:true,pagination:true,idField:'id',rownumbers:true">
        <thead data-options="frozen:true">
        <tr>
            <th data-options="checkbox:'true',field:'ck'"></th>
            <th data-options="align:'center',width:'120',field:'code'"><spring:message code="table.head.part.code"/></th>
            <th data-options="align:'center',width:'150',field:'name'"><spring:message code="table.head.part.name"/></th>
        </tr>
        </thead>
        <thead>
        <tr>
            <th data-options="align:'center',width:'140',field:'partType',formatter:partTypeFormatter"><spring:message code="table.head.part.part.type"/></th>
            <th data-options="align:'center',width:'140',field:'productType',formatter:productTypeFormatter"><spring:message code="table.head.part.product.type"/></th>
            <th data-options="align:'center',width:'140',field:'drawingCode'"><spring:message code="table.head.part.drawing.code"/></th>
            <th data-options="align:'center',width:'140',field:'drawingVersionCode'"><spring:message code="table.head.part.drawing.version.code"/></th>
            <th data-options="align:'center',width:'140',field:'supplierCode'"><spring:message code="table.head.part.supplier.code"/></th>
            <th data-options="align:'center',width:'140',field:'supplierName'"><spring:message code="table.head.part.supplier.name"/></th>
            <th data-options="align:'center',width:'140',field:'customerName'"><spring:message code="table.head.part.customer.name"/></th>
            <th data-options="align:'center',width:'140',field:'customerProjectCode'"><spring:message code="table.head.part.customer.project.code"/></th>
            <th data-options="align:'center',width:'140',field:'carModel'"><spring:message code="table.head.part.car.model"/></th>
            <th data-options="align:'center',width:'140',field:'comment'"><spring:message code="table.head.part.comment"/></th>
            <th data-options="align:'center',width:'140',field:'state',formatter:stateFormatter"><spring:message code="table.head.part.state"/></th>
        </tr>
        </thead>
    </table>
</div>
<div id="operateDialog" class="easyui-dialog"
     data-options="iconCls:'icon-save',resizable:true,modal:true,closed: true,collapsible:true,maximizable:true">
    <iframe scrolling="auto" id='operateIFrame' frameborder="0"  src="" style="width:100%;height:100%;"/>
</div>
</body>
</html>